package com.ax.base.system.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ax.base.common.domain.BaseArea;
import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.common.utils.PageUtils;
import com.ax.base.system.domain.MenuDO;
import com.ax.base.system.service.AreaService;

@RequestMapping("/sys/area")
@Controller
public class AreaController {
	@Autowired
	AreaService AreaService;
		
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String menu(Model model) {
		return "sys/area/area";
	}
	
	//树形
	@RequestMapping("/list")
	@ResponseBody
	Tree<MenuDO> arealist(){
		Tree<MenuDO> BaseAreas = AreaService.list();
		return BaseAreas;
	}
	
	/**
	 * 行政规划查询(模块:行政规划)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseArea>
	 */
	@RequestMapping("/queryArea")
	@ResponseBody
	PageUtils queryArealist(@RequestParam Map<String, Object> params){
		PageUtils BaseAreas = AreaService.queryArealistSer(params);
		return BaseAreas;
	}
	
	/**
	 * 行政规划新增(模块:行政规划)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return BaseArea
	 */
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", AreaService.getArea(pId).getAreaName());
		}
		return "sys/area/add";
	}
	
	/**
	 * 行政规划新增(模块:行政规划)
	 * 
	 * @param BaseArea baseArea
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertArea")
	@ResponseBody
	public AjaxRes insertArea(AjaxRes ajaxRes,BaseArea baseArea){
		try {
			this.AreaService.insertAreaSer(baseArea);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 行政规划修改跳转(模块:行政规划)
	 * 
	 * @param Long id
	 *           
	 * @return BaseArea baseArea
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("area",AreaService.getArea(id));
		return "sys/area/edit";
	}
	
	/**
	 * 行政规划修改(模块:行政规划)
	 * 
	 * @param BaseArea baseArea
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateArea")
	@ResponseBody
	public AjaxRes updateArea(AjaxRes ajaxRes,BaseArea baseArea){
		try {
			this.AreaService.updateAreaSer(baseArea);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * Tree
	 * 
	 * @param String parentAreaId
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree/{parentAreaId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("parentAreaId") Long parentAreaId) {
		Tree<MenuDO> tree = AreaService.getAreaTree(parentAreaId);
		return tree;
	}
	
}
